package com.ljy.utyun.sql;

import com.ljy.utyun.swagger.ControllerSwaggerProcessor;
import com.ljy.utyun.swagger.PojoSwaggerProcessor;
import com.ljy.utyun.swagger.SwaggerProcessor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * @author lijunying
 * @date 2020/10/16 9:24
 */
public class SqlFieldCollector {

    private String path;

    Map<String,Set<String>> fieldHashMap=new HashMap<>();

    public SqlFieldCollector(String path){
        this.path=path;
    }

    private List<String> getFileValueList(File file) throws IOException {
        BufferedReader br=new BufferedReader(new FileReader(file));
        List<String> valueList=new LinkedList<>();
        String line;
        while((line=br.readLine())!=null){
            valueList.add(line);
        }
        br.close();
        return valueList;
    }

    public Map<String,Set<String>> scanAndGetComment() throws IOException {
        return doScanAndGetComment(path);
    }

    private Map<String,Set<String>> doScanAndGetComment(String path) throws IOException {

        File file=new File(path);
        if(file.isDirectory()){
            for(String next:file.list()){
                fieldHashMap=doScanAndGetComment(path+"\\"+next);
            }
        }else{
            List<String> valueList= getFileValueList(file);
            collect(valueList);
        }
        return fieldHashMap;
    }

    private void collect(List<String> valueList){
        valueList.stream().forEach(l->{
            int index=-1;
            String startStr="  `";
            if((index=l.indexOf(startStr))!=-1){
                if(index==0){
                    index=index+startStr.length();
                    int end=l.indexOf("`",index);
                    String key=l.substring(index,end);
//                    System.out.println("key:"+key);
                    String commentStartStr="COMMENT '";
                    String commentEndStr="',";
                    int commentStartIndex=l.indexOf(commentStartStr);
                    if(commentStartIndex!=-1){
                        commentStartIndex+=commentStartStr.length();
                        int commentEndIndex=l.indexOf(commentEndStr,commentStartIndex);
                        String comment=l.substring(commentStartIndex,commentEndIndex);
                        putKeyValue(key,comment);
                    }
                }
            }
        });
    }

    private void putKeyValue(String key,String value){
        key=changeKey(key);
        Set<String> valueSet;
        if(!fieldHashMap.containsKey(key)){
            valueSet=new HashSet<>();
            fieldHashMap.put(key,valueSet);
        }else{
            valueSet=fieldHashMap.get(key);
        }
        value=value.toLowerCase();
        valueSet.add(value);
    }

    //将蛇形转成驼峰
    private String changeKey(String key){
        String[] array=key.split("_");
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<array.length;i++){
            String str=array[i];
            if(i!=0){
                str=str.substring(0,1).toUpperCase()+str.substring(1);
            }
            sb.append(str);
        }
        return sb.toString();
    }
}
